using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Gtk;
using XchangeStreamer.OutputSdk;
using XchangeStreamer.OutputSdk.ViewModel;
using System.Collections.Specialized;
using System.Timers;

public partial class MainWindow : Gtk.Window
{
    private Connector connector;

    public MainWindow()
        : base(Gtk.WindowType.Toplevel)
    {
        Build();

        #region bids

        #region bids_Price Column

        // Create a column for price
        Gtk.TreeViewColumn bids_Price = new Gtk.TreeViewColumn();
        bids_Price.Title = "Price";
        bids_Price.Resizable = true;
        // Create the text cell that will display price
        Gtk.CellRendererText bids_Price_Cell = new CellRendererText();
        // Add the cell to the column
        bids_Price.PackStart(bids_Price_Cell, true);

        #endregion

        #region bids_Quantity Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn bids_Quantity = new Gtk.TreeViewColumn();
        bids_Quantity.Title = "Quantity";
        bids_Quantity.Resizable = true;
        Gtk.CellRendererText bids_Quantity_Cell = new Gtk.CellRendererText();
        bids_Quantity.PackStart(bids_Quantity_Cell, true);

        #endregion

        #region bids_Splits Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn bids_Splits = new Gtk.TreeViewColumn();
        bids_Splits.Title = "Splits";
        bids_Splits.Resizable = true;
        Gtk.CellRendererText bids_Splits_Cell = new Gtk.CellRendererText();
        bids_Splits.PackStart(bids_Splits_Cell, true);

        #endregion

        // Add the columns to the TreeView
        bids.AppendColumn(bids_Price);
        bids.AppendColumn(bids_Quantity);
        bids.AppendColumn(bids_Splits);

        // Tell the Cell Renderers which items in the model to display

        bids_Price.AddAttribute(bids_Price_Cell, "text", 0);
        bids_Quantity.AddAttribute(bids_Quantity_Cell, "text", 1);
        bids_Splits.AddAttribute(bids_Splits_Cell, "text", 2);


        //Or

        //bids_Price.SetCellDataFunc(bids_Price_Cell, new Gtk.TreeCellDataFunc(Render_bids_Price_Cell));
        //bids_Quantity.SetCellDataFunc(bids_Quantity_Cell, new Gtk.TreeCellDataFunc(Render_bids_Quantity_Cell));
        //bids_Splits.SetCellDataFunc(bids_Splits_Cell, new Gtk.TreeCellDataFunc(Render_bids_Splits_Cell));


        #endregion

        #region asks

        #region asks_Price Column

        // Create a column for price
        Gtk.TreeViewColumn asks_Price = new Gtk.TreeViewColumn();
        asks_Price.Title = "Price";
        asks_Price.Resizable = true;
        // Create the text cell that will display price
        Gtk.CellRendererText asks_Price_Cell = new CellRendererText();
        // Add the cell to the column
        asks_Price.PackStart(asks_Price_Cell, true);

        #endregion

        #region asks_Quantity Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn asks_Quantity = new Gtk.TreeViewColumn();
        asks_Quantity.Title = "Quantity";
        asks_Quantity.Resizable = true;
        Gtk.CellRendererText asks_Quantity_Cell = new Gtk.CellRendererText();
        asks_Quantity.PackStart(asks_Quantity_Cell, true);

        #endregion

        #region asks_Splits Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn asks_Splits = new Gtk.TreeViewColumn();
        asks_Splits.Title = "Splits";
        asks_Splits.Resizable = true;
        Gtk.CellRendererText asks_Splits_Cell = new Gtk.CellRendererText();
        asks_Splits.PackStart(asks_Splits_Cell, true);

        #endregion

        // Add the columns to the TreeView
        asks.AppendColumn(asks_Price);
        asks.AppendColumn(asks_Quantity);
        asks.AppendColumn(asks_Splits);

        asks_Price.AddAttribute(asks_Price_Cell, "text", 0);
        asks_Quantity.AddAttribute(asks_Quantity_Cell, "text", 1);
        asks_Splits.AddAttribute(asks_Splits_Cell, "text", 2);


        #endregion

        #region trades

        #region trades_Time Column

        // Create a column for price
        Gtk.TreeViewColumn trades_Time = new Gtk.TreeViewColumn();
        trades_Time.Title = "Time";
        trades_Time.Resizable = true;
        // Create the text cell that will display price
        Gtk.CellRendererText trades_Time_Cell = new CellRendererText();
        // Add the cell to the column
        trades_Time.PackStart(trades_Time_Cell, true);

        #endregion

        #region trades_Quantity Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn trades_Quantity = new Gtk.TreeViewColumn();
        trades_Quantity.Title = "Quantity";
        trades_Quantity.Resizable = true;
        Gtk.CellRendererText trades_Quantity_Cell = new Gtk.CellRendererText();
        trades_Quantity.PackStart(trades_Quantity_Cell, true);

        #endregion

        #region trades_Price Column

        // Create a column for the Quantity
        Gtk.TreeViewColumn trades_Price = new Gtk.TreeViewColumn();
        trades_Price.Title = "Price";
        trades_Price.Resizable = true;
        Gtk.CellRendererText trades_Price_Cell = new Gtk.CellRendererText();
        trades_Price.PackStart(trades_Price_Cell, true);

        #endregion

        // Add the columns to the TreeView
        trades.AppendColumn(trades_Time);
        trades.AppendColumn(trades_Price);
        trades.AppendColumn(trades_Quantity);

        trades_Time.AddAttribute(trades_Time_Cell, "text", 4);
        trades_Price.AddAttribute(trades_Price_Cell, "text", 2);
        trades_Quantity.AddAttribute(trades_Quantity_Cell, "text", 3);

        #endregion

        Timer t = new Timer(60000);
        t.AutoReset = false;
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Start();
    }

    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        Task.Factory.StartNew(this.Connect);
    }

    private void Connect()
    {
        this.connector = Connector.Connection;
        this.connector.Connect();

        this.Bind();
    }

    private void Bind()
    {
        this.bids.Model = (TreeModelSort)this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Bids;
        this.asks.Model = (TreeModelSort)this.connector.Market.Find("EGS74081C018").MarketDepthByPrice_Asks;
        this.trades.Model = (TreeModelSort)this.connector.Market.Find("EGS74081C018").Trades;

        ((TreeModelSort)this.bids.Model).SetSortColumnId(0, SortType.Descending);
        ((TreeModelSort)this.asks.Model).SetSortColumnId(0, SortType.Ascending);
        ((TreeModelSort)this.trades.Model).SetSortColumnId(4, SortType.Descending);
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        this.connector.Disconnect();
        this.connector.Dispose();

        Application.Quit();
        a.RetVal = true;
    }
}